﻿/********************************************************************************/
/* 	enemy.d																		*/
/*------------------------------------------------------------------------------*/
/*	製作		（　゜ワ゜）ノ　／　松久貫太									*/
/*	製作開始　	2007/05/11														*/
/*	MAIL		omega@personal.email.ne.jp										*/
/*	URL		http://nagoya.cool.ne.jp/o_mega										*/
/*																				*/
/*	このソースは「やらわかライセンス」の元で配布されています。					*/
/*-更新履歴---------------------------------------------------------------------*/
/*	2007/08/04 製作開始															*/
/*-その他-----------------------------------------------------------------------*/
/*	なし																		*/
/********************************************************************************/

private import hell2;
private import actor;
private import vector;
private import screen;
private import player;
private import effect;
private import shot;
private import state;
private import std.math;

private import defines;


class EnemyActor : ActorPool!(Enemy)
{
public:
	GameMain gamemain;
	
	this(GameMain gm,int n)
	{
		gamemain = gm;
		super(n);
	}
	
	void add(Vec3 p ,Vec3 v,EnemySpec esp)
	{
		Enemy s = this.getInstance();
		if(s)
		{
			s.init(gamemain,p,v,esp);
		}
	}
	
	Enemy getNearestEnemy(Vec3 v)
	{
		float dist = 99999999.0;
		Enemy result = null;
		
		foreach(Enemy s;actors)
		{
			if(s.exist)
			{
				float d = s.position.getDistSqrt(v);
				if(d < dist)
				{
					dist = d;
					result = s;
				}
			}
		}
		return result;
	}
}

class EnemySpec
{
	double size;
	
	this(){
		size = 4.0;
	}
}

/* */
class Enemy : Actor
{
public:
	Vec3 speed;
	Vec3 rotation;
	Vec3 rot_speed;
	double direction;
	int timer;
	GameMain gamemain;
	int life;
	
	EnemySpec spec;
	
	this()
	{
		super();
	}
	
	void init(GameMain gm,Vec3 p,Vec3 v,EnemySpec ep)
	{
		gamemain = gm;
		exist = true;
		position = new Vec3(p);
		speed = new Vec3(v);
		rotation = new Vec3();
		rot_speed = new Vec3( Hell_randInt(-100,100) / 4.0 ,
						Hell_randInt(-100,100) / 4.0 ,
						Hell_randInt(-100,100) / 4.0 ) * 0.1;
		this.spec = ep;
		this.direction = v.atan2D();
		timer = 0;
		life = 2;
		if(Hell_randInt(0,100) < 5) life = 2 * 8;
		
		position.z = CHAR_Z_SURFACE;
	}
	
	void move()
	{
		if(!exist) return;
		
		position += speed;
		rotation += rot_speed;
		timer++;
		
		if(position.x > CHAR_X_ABSMAX + 2)
		{
		//	position.x = CHAR_X_ABSMAX + 2;
			if(speed.x > 0) speed.x *= -1.0;
		}
		if(position.x < -CHAR_X_ABSMAX - 2)
		{
		//	position.x = -CHAR_X_ABSMAX - 2;
			if(speed.x < 0) speed.x *= -1.0;
		}
		if(position.y > CHAR_Y_ABSMAX + 2)
		{
		//	position.y = CHAR_Y_ABSMAX + 2;
			if(speed.y > 0) speed.y *= -1.0;
		}
		if(position.y < -CHAR_Y_ABSMAX - 2)
		{
		//	position.y = -CHAR_Y_ABSMAX - 2;
			if(speed.y < 0) speed.y *= -1.0;
		}
		
		this.direction = speed.atan2D();
		// collision
		if(position.x > CHAR_X_ABSMAX + 2 || position.x < -CHAR_X_ABSMAX - 2
			 || position.y > CHAR_Y_ABSMAX + 2 || position.y < -CHAR_X_ABSMAX - 2)
		{
			return;
		}
		Shot s = gamemain.shotactor.getNearestShot(position);
		if(s)
		{
			if(s.position.getDist(position) < getSize() * 1.5)
			{
				// Damage
				life--;
				
				position += s.speed * 0.1; // ノックバック
//				speed += s.speed * 0.002;
				rotation.x += Hell_randInt(-20,20) / 1.0;
				rotation.y += Hell_randInt(-20,20) / 1.0;
				rotation.z += Hell_randInt(-20,20) / 1.0;
				s.destory();
				
				
				if(life < 1){
					destroy();
				}
			}
		}
	}
	
	void draw()
	{
		glPushMatrix();
		{
			glTranslatef(position.x , position.y ,position.z);
			glRotatef(rotation.x , rotation.y , rotation.z);
			
			float sz = getSize();
			float r = 1.0 , g = 1.0 , b = 1.0;
			if(life > 4 & timer % 6 < 3){
				r = 1.0;
				g = 0.6;
				b = 0.2;
			}
			glScalef(sz , sz , sz);
			glColor4f(r , g , b , 0.5);
			CubeShape.drawPolygon();
			glColor4f(r , g , b , 0.8);
			glScalef( 0.9 , 0.9 , 0.9 );
			CubeShape.drawWire();
		}
		glPopMatrix();
		
		glPushMatrix();
		{
			glTranslatef(position.x , position.y ,position.z);
			glRotatef(0 , 0 , direction * 180 / 3.141592);
			glTranslatef( 2 , 0 , 0 );
			glRotatef(timer * 5,0 ,0);
			
			glScalef( 0.5 , 0.5, 0.5 );
			glColor4f(0.1 , 1.0 , 0.1 , 0.6);
			DeltaShape.drawWire();
		}
		glPopMatrix();
		/*
		resetPerspective();
		Hell_drawFont(std.string.toString(cast(int)(direction * 100) / 100.0),
			cast(int)(1024/2 + position.x * 15) ,
			cast(int)(768/2 + position.y * 10) ,0.5 );
		setPerspective();
		*/
	}
	
	float getSize()
	{
		return 1.0;
	}
	
	void destroy(){
		exist = false;
		
		for(int t=0;t<24;t++)
		{
			Vec3 p = new Vec3(position);
			p.x += Hell_randInt(-10,10) / 5.0;
			p.y += Hell_randInt(-10,10) / 5.0;
			gamemain.addEffect(p , new Vec3() ,
				EFFECTTYPE.TRIANGLE);
		}
		for(int t=0;t<256;t++)
		{
			Vec3 p = new Vec3(position);
			p.x += Hell_randInt(-10,10) / 5.0;
			p.y += Hell_randInt(-10,10) / 5.0;
			double r = Hell_randInt(0,360) / 180.0 * 3.141592;
			double rr = Hell_randInt(0,360) / 180.0 * 3.141592;
			gamemain.addEffect(p , new Vec3(cos(r) * cos(rr) /2, sin(r) * cos(rr)/2 , sin(rr)/2) ,
				EFFECTTYPE.POINT);
		}
	}
}